package xjtu.zd.server.normal.login.impl;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xjtu.zd.server.common.CommonService;
import xjtu.zd.server.normal.login.LoginDto;
import xjtu.zd.server.normal.login.LoginService;
import xjtu.zd.server.normal.login.User;
import xjtu.zd.server.normal.login.dao.LoginDao;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;

@Service
@Transactional
public class LoginServiceImpl implements LoginService {

    @Resource
    private LoginDao loginDao;
    @Resource
    private CommonService commonService;

    @Override
    public String findIdByPwd(LoginDto dto) {
        return loginDao.findIdByPwd(dto);
    }

    @Override
    public User findUserByEmail(LoginDto dto) {
        return loginDao.findIdByEmail(dto);
    }

    @Override
    public boolean checkSignUp(LoginDto dto) {
        int cnt = loginDao.findCountByNameAndEmail(dto);
        return !(cnt > 0);
    }

    @Override
    public void addNewUser(LoginDto dto) {
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);					//放入Date类型数据

        dto.setU_id(commonService.returnNewId(a -> loginDao.findNewUserId(a)));
        dto.setU_time(date);

        loginDao.insertUser(dto);
    }

    @Override
    public void resetPwd(LoginDto dto) {
        loginDao.updatePwdByEmail(dto);
    }

}
